package com.jm.product.entity.dto;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

public class Attribute {

	private String name;
	private String options;
	private int type;
	private String value;
	private List<Object> valueList;
	private int[] nums = null;
	private int hidden;
	private int required;
	private String datatype;
	private String unit;
	private Map<String, Object>[] maps;
	
	public Attribute() {
	    this.valueList = new ArrayList<Object>();
	}
	public void addValue(String _value) {
	    this.valueList.add(_value);
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getOptions() {
		return options;
	}
	public void setOptions(String options) {
		this.options = options;
	}
	public String[] getOptionAr() {
	    if ((this.options == null) || (this.options.equals(""))) {
	      return new String[0];
	    }
	    String[] ar_options = this.options.split(",");
	    
	    return ar_options;
	  }
	  
	  @SuppressWarnings("unchecked")
	public Map<String, Object>[] getOptionMap() {
	    String[] optionAr = getOptionAr();
	    if (this.maps == null) {
	      this.maps = new Map[optionAr.length];
	      for (int i = 0; i < optionAr.length; i++) {
	        Map<String, Object> m = new HashMap<String, Object>(4);
	        m.put("name", optionAr[i]);
	        if(getNums() != null && getNums().length == optionAr.length){
	        	getNums()[i] = i ;
	        	m.put("num", getNums()[i]);
	        }
	        m.put("url", "");
	        if(StringUtils.isNotBlank(value) && Integer.parseInt(value) == i){
	        	m.put("selected", 1);
	        } else {
	        	m.put("selected", 0);
	        }
	        this.maps[i] = m;
	      }
	    }
	    return this.maps;
	  }
	public int getType() {
		return type;
	}
	public void setType(int type) {
		this.type = type;
	}
	public String getValue() {
		return value;
	}
	public void setValue(String value) {
		this.value = value;
	}
	public String getValStr() {
	    if (this.type >= 3) {
	      if ((this.type >= 3) && (this.type <= 5)) {
	        if ((this.value != null) && (!"".equals(this.value)) && (!"null".equals(this.value))) {
	          this.value = StringUtils.isBlank(this.value) ? "0" :this.value ;
	          int index1 = Integer.parseInt(this.value);
	          if (getOptionAr().length > index1) {
	            return getOptionAr()[index1];
	          }
	        } else {
	          return null;
	        }
	      } else if (this.type == 6) {
	        if ((this.value != null) && (!this.value.equals("")) && (!this.value.equals("null"))) {
	          String[] ids = this.value.substring(1).split("#");
	          String result = "";
	          for (String id : ids) {
	            if (!StringUtils.isEmpty(result)) {
	              result = result + ",";
	            }
	            result = result + getOptionAr()[Integer.parseInt(id)];
	          }
	          return result;
	        }
	        return null;
	      }
	    }
	    return this.value;
	  }
	public List<Object> getValueList() {
		return valueList;
	}
	public void setValueList(List<Object> valueList) {
		this.valueList = valueList;
	}
	public int[] getNums() {
		if (this.nums == null) {
		      this.nums = new int[getOptionAr().length];
		}
		return this.nums;
	}
	public void setNums(int[] nums) {
		this.nums = nums;
	}
	public int getHidden() {
		return hidden;
	}
	public void setHidden(int hidden) {
		this.hidden = hidden;
	}
	public int getRequired() {
		return required;
	}
	public void setRequired(int required) {
		this.required = required;
	}
	public String getDatatype() {
		return datatype;
	}
	public void setDatatype(String datatype) {
		this.datatype = datatype;
	}
	public String getUnit() {
		return unit;
	}
	public void setUnit(String unit) {
		this.unit = unit;
	}
	@Override
	public String toString() {
		return "Attribute [name=" + name + ", options=" + options + ", type=" + type + ", value=" + value
				+ ", valueList=" + valueList + ", nums=" + Arrays.toString(nums) + ", hidden=" + hidden + ", required="
				+ required + ", datatype=" + datatype + ", unit=" + unit + ", maps=" + Arrays.toString(maps) + "]";
	}
}
